# Author: Zako888y
#
# This file is dedicated to the public domain under the Creative Commons CC0 1.0 Universal (CC0 1.0) Public Domain Dedication.
# You may use, copy, modify, distribute, and/or sell this code for any purpose, without restriction. No warranty is provided. The author waives all rights and claims to this work worldwide.
# To view a copy of this dedication, visit https://creativecommons.org/publicdomain/zero/1.0/

"""
此程序已失败——无法通过安全检测。

请手动 F12 打开浏览器控制台，进入“网络”选项卡，刷新页面，
找到请求 URL 为 `https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo` 的请求
（用“搜索”——“查找”，关键字 `getDanmuInfo`），
查看“响应”内容。响应的内容应该是 json 格式的。在 "data" 值中找到 "token" 键对应的值。

token 示例：`FxZ1jWO11ZN0Vsun_bmSf9Upw1fKX7GiE53ECbs6Z_2WBVaq_8efWGEChviGlsiqKxB7W1vzgmjAJG49owQn7fyMxxIqKBYEWuvrr4ER9ltiWDKTx3zQY6SZpzgdjq3mPlV1Kuy_DaZ78a1HRK4HVi0tIwDryciuaMxa32weOlbtt95oVd-hS9BnFVvrith6REAnfRSGrNjFm61aZ_TLjW5JpYuZFzXL9zWjptAXXc81TtzRfxweZLiMvOUJ0A==`

token 似乎与 (访客, 直播间) 有关。例如：进入直播间 123456789 查到的 token 不能适用于 123456788。
猜测 token 有一定时效。但用它成功接入弹幕 ws 流后，流可以使用似乎超过 24 小时，并且似乎仍可用相同 token 重新连接。

"""

import requests


def get_danmu_info():
    url = "https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo"
    params = {
        "id": "22849290",
        "type": "0",
        # "web_location": "444.8",
        # "w_rid": "29fb655b182730a3bc08466ac68dc225",
        # "wts": "1754759190",
    }
    response = requests.get(url, params=params)
    print("Status Code:", response.status_code)
    try:
        data = response.json()
        # 提取token字段
        token = data.get("data", {}).get("token")
        print("Token:", token)
    except Exception:
        print("Response:", response.text)


if __name__ == "__main__":
    get_danmu_info()
